
#include <string.h>
#include "BC26.h" 
#include "stdio.h" 
#include "usart.h"

#include "TelecomPlatform.h"	
#include "main.h"

extern unsigned char UART1_Rx_Buf[MAX_REC_LENGTH];
extern unsigned int  UART1_Rx_cnt ;
extern unsigned char UART1_temp[REC_LENGTH];




/***********************
���õ��� loT ƽ̨��ַ�Ͷ˿�
*************************/
uint8_t BC26_QLWSERV(uint8_t *TelecomPlatformIP,uint8_t*TelecomPlatformPort)
{ 
	uint8_t i=0,sockedid=0,result=0 ;
	uint8_t ATStrBuf[50]={0};
	const uint8_t Buf1[2]={'"',0 };
	const uint8_t Buf2[3]={'"',',',0};
	SysPrintf("\n--->>>Config IP and Port...\n");
	
	sprintf(ATStrBuf,"AT+QLWSERV=");
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,TelecomPlatformIP);
	strcat(ATStrBuf,Buf2);
	strcat(ATStrBuf,TelecomPlatformPort);
	strcat(ATStrBuf,"\r\n\0");
	SysPrintf("ATStrBuf=%s\n",ATStrBuf);

	
	i=3;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART1_Rx_Buf);
		if((strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("BC26_QLWSERV success\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);	
		}		
	}
		
	SysPrintf("BC26_QLWSERV fail\n");
	return FALSE;
	
}

/***********************
���õ��� loT ƽ̨����.���豸�� IMEI �ŵ�
*************************/
uint8_t BC26_QLWCONF(uint8_t *IMEI)
{ 
	uint8_t i=0,sockedid=0,result=0 ;
	uint8_t ATStrBuf[50]={0};
	const uint8_t Buf1[2]={'"',0 };
	SysPrintf("\n--->>>Config Equipment IMEI\n");
	
	sprintf(ATStrBuf,"AT+QLWCONF=");
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,IMEI);
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,"\r\n\0");
	SysPrintf("ATStrBuf=%s\n",ATStrBuf);
	
	i=3;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART1_Rx_Buf);
		if((strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("BC26_QLWCONF success\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);	
		}		
	}
		
	SysPrintf("BC26_QLWCONF fail\n");
	return FALSE;
	
}

/***********************
	���� LwM2M ����
*************************/
uint8_t BC26_QLWADDOBJ(void)
{ 
	uint8_t i=0,sockedid=0,result=0;
	
	uint8_t  ATStrBuf_UPchannle[25+1]={'A','T','+','Q','L','W','A','D','D','O','B',\
		                             'J','=','1','9',',','0',',','1',',','"','0','"','\r','\n',0};
	uint8_t  ATStrBuf_Downchannle[25+1]={'A','T','+','Q','L','W','A','D','D','O','B',\
		                             'J','=','1','9',',','1',',','1',',','"','0','"','\r','\n',0};	
	SysPrintf("\n--->>>add LwM2M obj\n");
	
	i=3;
	while(i)
	{
	    --i;
		UART1_Rx_cnt=0 ;
		memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf_UPchannle,strlen(ATStrBuf_UPchannle));
		HAL_Delay(1000);
		
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART1_Rx_Buf);
		if((strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("BC26_QLWADDOBJ up success\n");
			
			break;
		}	
		else
		{
			HAL_Delay(2000);	
		}		
	}
		
	if(i==0)
	{
		SysPrintf("BC26_QLWADDOBJ up fail\n");	
	}
	

	i=3;
	while(i)
	{
		--i;
		UART1_Rx_cnt=0 ;
		memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf_Downchannle,strlen(ATStrBuf_Downchannle));
		HAL_Delay(1000);
		
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART1_Rx_Buf);
		if((strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("BC26_QLWADDOBJ down success\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);	
		}		
	}
	SysPrintf("BC26_QLWADDOBJ down fail\n");	
	return  FALSE; 
			
}


/***********************
  ����� loT ƽ̨����ע������
*************************/
uint8_t BC26_QLWOPEN(void)
{ 
	uint8_t i=0,sockedid=0,result=0,j=0;
	uint8_t  ATStrBuf[14+1]={'A','T','+','Q','L','W','O','P','E','N','=','1','\r','\n',0};
	SysPrintf("\n--->>>Send Registration request\n");
	i=2;
	while(i)
	{
		--i;
		UART1_Rx_cnt=0 ;
		memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		
		if((strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("BC26 Regist  success\n");
			/*************
			len=26
			AT+QLWOPEN=1
			OK
			
			CONNECT OK  
			��ʱ��ܿ��յ� CONNECT OK
			*************/
			if(strstr(UART1_Rx_Buf,"CONNECT OK")==NULL)
			{
				j=0;
				UART1_Rx_cnt=0 ;
				memset(UART1_Rx_Buf,0,Uart_ReceBufLen);			
				while(1)
				{
					HAL_Delay(1000);
					SysPrintf("*");
					if(++j>128)
					{
						break;
					}
					if(UART1_Rx_cnt>0)
					{
						HAL_Delay(1000);
						
						UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
						SysPrintf("%s\n",UART1_Rx_Buf);
						if(strstr(UART1_Rx_Buf,"CONNECT OK")!=NULL)
						{
								break;
						}
						else if(strstr(UART1_Rx_Buf,"CONNECT FAIL")!=NULL)
						{
								return  FALSE; 	
						}
						else
						{
							UART1_Rx_cnt=0 ;
							memset(UART1_Rx_Buf,0,Uart_ReceBufLen);	

						}
						
					}
				}
			}


			
			j=0;
			UART1_Rx_cnt=0 ;
			memset(UART1_Rx_Buf,0,Uart_ReceBufLen);	
			while(1)
			{
				HAL_Delay(1000);
				SysPrintf("*");
				if(++j>60)
				{
					break;
				}
				if(UART1_Rx_cnt>0)
				{
					HAL_Delay(1000);
					SysPrintf("--len=%d\n",UART1_Rx_cnt);
					UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
					SysPrintf("%s\n",UART1_Rx_Buf);
					if(strstr(UART1_Rx_Buf,"+QLWOBSERVE:")!=NULL)
					{
						return TRUE;
					}
					else
					{
						UART1_Rx_cnt=0 ;
						memset(UART1_Rx_Buf,0,Uart_ReceBufLen);		

					}

				}
			}			
			
		}
		else
		{
			HAL_Delay(2000);	
		}		
	}
	SysPrintf("BC26 Regist fail\n");
	return  FALSE; 
			
}



/***********************
 ����� IoT ƽ̨���͸�������
*************************/
uint8_t BC26_QLWUPDATE(void)
{ 
	uint8_t i=0,sockedid=0,result=0,j=0;
	uint8_t  ATStrBuf[14+1]={'A','T','+','Q','L','W','U','P','D','A','T','E','\r','\n',0};

	SysPrintf("\n--->>>Send update request\n");
	
	i=2;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(500);
		
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		
		if((strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("QLWUPDATE success\n");


			if(strstr(UART1_Rx_Buf,"UPDATE OK")==NULL)
			{
				j=0;
				UART1_Rx_cnt=0 ;
				memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
				
				while(1)
				{
					HAL_Delay(1000);
					SysPrintf("*");
					if(++j>60)
					{
						break;
					}
					if(UART1_Rx_cnt>0)
					{
						HAL_Delay(1000);
						
						UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
						SysPrintf("%s\n",UART1_Rx_Buf);
						if(strstr(UART1_Rx_Buf,"UPDATE OK")!=NULL)
						{
							SysPrintf("update success\n");
							return TRUE;
						}
						else	if(strstr(UART1_Rx_Buf,"UPDATE FAIL")!=NULL)
						{	
							SysPrintf("update fail\n");
							return FALSE;
						}	
						

					}
				}
			}
			else
			{
				SysPrintf("Update Success\n");
				return TRUE;

			}
			
			
		}
		else
		{
			HAL_Delay(2000);	
		}
		
	}
		
	SysPrintf("BC26_QLWUPDATE fail\n");
	return FALSE;
	
}



/***********************
 ���ÿ�ѡ����
 ��������dataformat
 0 Ϊ�ı��ַ�����1λ16�����ַ���
*************************/
uint8_t BC26_QLWCFG(void)
{ 
	uint8_t i=0,sockedid=0,result=0 ;
	
	uint8_t  ATStrBuf[28+1]={'A','T','+','Q','L','W','C','F','G','=','"','d','a',\
	't','a','f','o','r','m','a','t','"',',','0',',','0','\r','\n',0};

	SysPrintf("\n--->>>Config dataformat\n");
	
	i=3;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART1_Rx_Buf);
		if((strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("AT+QLWCFGsuccess\n");
			
			return TRUE;
		}	
		else
		{
			HAL_Delay(2000);	
		}
	}
		
	SysPrintf("AT+QLWCFG fail\n");
	return FALSE;
	
}


/************************************************************
	�������ݵ�ƽ̨
	����iotƽ̨������ͨ����19/1/0,����ͨ��19/0/0
**************************************************************/
uint16_t SendDataToTelecomPlatform(uint8_t*buffer,uint16_t len)
{
    uint8_t i=3,j=0;
    uint8_t ATStrBuf[150]={0};
    sprintf(ATStrBuf,"AT+QLWDATASEND=19,0,0,%d,%s,0x0100\r\n",len,buffer);
    while(i--)
    {
    	
        UART1_Rx_cnt=0;
		memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
        SendDataToNBModule(ATStrBuf,strlen(ATStrBuf)+1);
			SysPrintf(">>>>>>>>%s\r\n",ATStrBuf);
        HAL_Delay(500);
        
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		
        if((strstr(UART1_Rx_Buf,"OK")!=NULL))
        
       
        {
        	

			if(strstr(UART1_Rx_Buf,"SEND OK")==NULL)
			{
				UART1_Rx_cnt=0 ;
				memset(UART1_Rx_Buf,0,Uart_ReceBufLen);        	
	            
				j=30;
				while(j--)
				{
					SysPrintf("*");
					HAL_Delay(1000);
					if(UART1_Rx_cnt>0)
					{
						
						UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
						
					  
						if(strstr(UART1_Rx_Buf,"SEND OK")!=NULL)
						{
							 SysPrintf("send  data success\n");
							 return	TRUE;
						}	
						else if(strstr(UART1_Rx_Buf,"SEND FAIL")!=NULL)
						{
							SysPrintf("send  data fail\n");
							return FALSE;
						}
					}
				}
			}
			else
			{
				SysPrintf("Send  Data Success\n");
				return	TRUE;
			}

        }
        else 
        {
			/*************************************************
			---->>>Send  ERR
			ATASEND=19,0,0,61,866971038866583898611202090135440450cc1c101010001440e20030000,0x0100
			ERROR
			*************************************************/
            SysPrintf("---->>>Send  ERR\n");
			/*for(h=0;h<UART1_Rx_cnt;h++)
			{
				SysPrintf("%c",UART1_Rx_Buf[h]);
			}
			SysPrintf("\n");*/
			
			HAL_Delay(2000);
        }
        
    }

    return FALSE ;
}


/***********************
 ����ƽ̨����,���ݳ�������Ϊ100
*************************/

uint16_t	ReceiveMessageFromTelecomPlatform(uint8_t*recebuffer)
{ 

	uint8_t i=0,sockedid=0,len=0 ; 
	uint8_t  ATStrBuf[14+1]={'A','T','+','Q','L','W','R','D','=','1','0','0','\r','\n',0};
	uint8_t *start;

	SysPrintf("\n--->>>Receive Data from Telecom\n");
	/******************
	AT+QLWRD=100
	+QLWRD: 12,0
	111111111111

	OK
	***************/
	i=6;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(700);
		LED_blink(2);
		if((strncmp(UART1_Rx_Buf,"AT+QLWRD=100",strlen("AT+QLWRD=100"))==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			start = strstr(UART1_Rx_Buf, "+QLWRD:");
			if (start != NULL)
			{
				len=UtilGetIntFlag(start,"+QLWRD:",",");
				if(len>0)
				{
					
					start = strstr(UART1_Rx_Buf, ",");
					if (start != NULL)
					{
						start = strstr(start, "\r\n");
						if (start != NULL)
						{	
						
							memcpy(recebuffer,start+strlen("\r\n"),len);
							recebuffer[len]=0;
							SysPrintf("data=%s\n",recebuffer);
							return len;
						}
					}
					
				}
				
				
			} 				
		}		
	}
	
	SysPrintf("NO data to receive\n");
	return 0;

}
/***********************
	ɾ�� LwM2M ����
 	�����쳣�����Ҫ����������ز�������Ӧ������֮ǰִ�� AT+QLWDEL��
 	
	AT+QLWDEL
	OK

	AT+QLWDEL success 	
*************************/
uint8_t BC26_QLWDEL(void)
{ 
	uint8_t i=0,sockedid=0,result=0;
	
	uint8_t  ATStrBuf[11+1]={'A','T','+','Q','L','W','D','E','L','\r','\n'};
	SysPrintf("\n--->>>ɾ�� LwM2M ����\n");
	i=3;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		memset(UART1_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(2000);
		
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART1_Rx_Buf);
		if((strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("AT+QLWDEL success\n");
			
			return TRUE;
		}
		else if((strstr(UART1_Rx_Buf,"ERROR")!=NULL))
		{
			SysPrintf("AT+QLWDEL fail\n");
			
			return FALSE;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
		
	SysPrintf("AT+QLWDEL fail\n");
	return FALSE;
	
}

